layui.use(['layer', 'form', 'laypage'], function () {
  var layer = layui.layer, form = layui.form, layPage = layui.laypage, basePath = window.location.pathname
  var m = $('meta[name=\'_csrf\']').attr('content')
  var n = $('meta[name=\'_csrf_header\']').attr('content')
  /**page分页**/
  var count = $('input[name=\'totalElements\']').eq(0).val()//一共多少条数据
  layPage.render({
    elem: 'layPage',
    count: parseInt(count),
    curr: parseInt($('input[name=\'current\']').eq(0).val()),
    groups: 5, //连续显示分页数
    theme: '#1e9fff',
    limit: parseInt($('input[name=\'rows\']').eq(0).val()),
    limits: [50, 100],
    skip: true,
    jump: function (obj, first) {//获取当前选中的页
      $('input[name=\'current\']').eq(0).val(obj.curr)
      $('input[name=\'rows\']').eq(0).val(obj.limit)
      if (!first) {
        $('.myui-form-search').eq(0).submit()
      }
    }
  })
  /**全选，全不选***/
  form.on('checkbox(allChoose)', function (data) {
    $('.choose').prop('checked', data.elem.checked)
    form.render('checkbox')
  })
  /**通过单选按钮控制全选**/
  form.on('checkbox(choose)', function (data) {
    var check = false
    if (data.elem.checked) {//如果选中
      check = true
    } else {
      $('.choose').each(function (i) {
        if ($(this).prop('checked') == true) {
          check = true
        }
      })
    }
    $('.allChoose').prop('checked', check)
    form.render('checkbox')
  })
  basePath = basePath.substring(0, basePath.lastIndexOf('/'))
  /**新增***/
  $('.layui-add').on('click', function () {
    layer.open({
      type: 2,
      title: '新增',
      shadeClose: true,
      shade: 0.2,
      maxmin: true, //开启最大化最小化按钮
      area: ['70%', '80%'],
      content: basePath + '/new'
    })
  })
  /**修改***/
  $('.layui-update').on('click', function () {
    var ids = changeIdsValue()
    console.log(ids)
    if ('' == ids) {
      layer.msg('请勾选修改项', {time: 3000, icon: 5})
      return
    }
    if (ids.indexOf(',') != -1) {
      layer.msg('最多只能勾选一项', {time: 3000, icon: 5})
      ids = ''
      return
    }
    layer.open({
      type: 2,
      title: '修改',
      shadeClose: true,
      shade: 0.2,
      maxmin: true, //开启最大化最小化按钮
      area: ['70%', '80%'],
      content: basePath + '/' + ids + '/edit',
    })
    ids = ''
  })
  /**查看**/
  $('.layui-open').on('click', function () {
    var ids = changeIdsValue()
    console.log(ids)
    if ('' == ids) {
      layer.msg('请勾选查看项', {time: 3000, icon: 5})
      return
    }
    if (ids.indexOf(',') != -1) {
      layer.msg('最多只能勾选一项', {time: 3000, icon: 5})
      ids = ''
      return
    }
    layer.open({
      type: 2,
      title: '查看',
      shadeClose: true,
      shade: 0.2,
      maxmin: true, //开启最大化最小化按钮
      area: ['70%', '80%'],
      content: basePath + '/' + ids + '/entity',
    })
    ids = ''
  })
  /**删除***/
  $('.layui-del').on('click', function () {
    var ids = changeIdsValue()
    console.log(ids)
    if ('' == ids) {
      layer.msg('请勾选删除内容', {time: 3000, icon: 5})
      return
    }
    var jsonData = {'ids': ids}
    $.ajax({
      type: 'POST',
      url: basePath + '/delete',
      data: JSON.stringify(jsonData),
      dataType: 'json',
      async: false,
      beforeSend: function (xhr) {xhr.setRequestHeader(n, m)},
      contentType: 'application/json;charset=utf-8',
      error: function (request) {
        console.log('Connection error')
      },
      success: function (data) {
        if (data.state == true) {
          window.location.reload(true)
          layer.msg('操作成功！', {time: 3000, icon: 4})
        }
      }
    })
  })

  $(".list-root li").after("<li class=\"list-divider\"></li>");

  /**获取勾选项值集合***/
  function changeIdsValue () {
    var val = ''
    $('.choose').each(function (i, item) {
      if ($(item).prop('checked') == true) {
        var info = $(item).parents('td').next().html()
        if (info == '' || undefined == info) {
          info = $(item).val()
        }
        val = val + info + ','

      }
    })
    return val.substring(0, val.lastIndexOf(','))
  }
})
    